package com.android.RssReader.Screen;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.RssReader.R;
import com.android.RssReader.RssReaderApplication;
import com.android.RssReader.View.ScrollLayout;
import com.android.RssReader.View.ScrollLayout.OnScreenChangeListener;
import com.android.RssReader.View.ScrollLayout.OnScreenChangeListenerDataLoad;

public class ScreenNews extends Screen{
	private ScrollLayout mScrollLayout;
	private DataLoading dataLoad;
	private TextView newsNum;
	private ScreenArgs screenArgs;
	
	public ScreenNews() {
		super(SCREEN_TYPE.SCREEN_NEWS_T, ScreenNews.class.getCanonicalName());
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		RssReaderApplication.LogD(ScreenNews.class, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_news);
		screenArgs = (ScreenArgs) getIntent().getSerializableExtra("args");
		dataLoad = new DataLoading();
		mScrollLayout = (ScrollLayout)findViewById(R.id.ScrollLayout);
		newsNum = (TextView)findViewById(R.id.news_num);
		mScrollLayout.addView(creatChildView(0));
		mScrollLayout.addView(creatChildView(1));
		mScrollLayout.addView(creatChildView(2));
		mScrollLayout.addView(creatChildView(3));
		dataLoad.bindScrollViewGroup(mScrollLayout);
		mScrollLayout.setOnScreenChangeListener(new OnScreenChangeListener() {
			@Override
			public void onScreenChange(int currentIndex) {
				RssReaderApplication.LogD(ScreenNews.class, "currentIndex---:" + currentIndex);
				newsNum.setText("NO:" + (currentIndex + 1));
			}
		});
	}

	@Override
	protected void onDestroy() {
		RssReaderApplication.LogD(ScreenNews.class, "onDestroy");
		super.onDestroy();
	}

	
	//分页数据
	class DataLoading {
		private int count;
		public void bindScrollViewGroup(ScrollLayout scrollViewGroup) {
			this.count=scrollViewGroup.getChildCount();
			scrollViewGroup.setOnScreenChangeListenerDataLoad(new OnScreenChangeListenerDataLoad() {
				@Override
				public void onScreenChange(int currentIndex) {
					generatePageControl(currentIndex);
				}
			});
		}
		
		private void generatePageControl(int currentIndex){
			RssReaderApplication.LogD(ScreenNews.class, "count:" + count);
			RssReaderApplication.LogD(ScreenNews.class, "currentIndex:" + currentIndex);
			//如果快到最后一页,就加载新的数据
			if(count==currentIndex+2){
				RssReaderApplication.LogD(ScreenNews.class, "data add");
				mScrollLayout.addView(creatChildView(count));
				mScrollLayout.addView(creatChildView(count +1));
				dataLoad.bindScrollViewGroup(mScrollLayout);
			}
		}
	}
	
	private View creatChildView(int i) {
		LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.screen_news_content, null);
		TextView title = (TextView) layout.findViewById(R.id.screen_news_title);
		TextView date = (TextView) layout.findViewById(R.id.screen_news_date);
		ImageView image = (ImageView) layout.findViewById(R.id.screen_news_image);
		TextView content = (TextView) layout.findViewById(R.id.screen_news_content);
		return layout;
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		RssReaderApplication.LogD(ScreenNews.class, "onNewIntent");
		screenArgs = (ScreenArgs) getIntent().getSerializableExtra("args");
		
	}

	@Override
	protected void onPause() {
		super.onPause();
		RssReaderApplication.LogD(ScreenNews.class, "onPause");
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		RssReaderApplication.LogD(ScreenNews.class, "onResume");
		
	}

	@Override
	protected void onStart() {
		super.onStart();
		RssReaderApplication.LogD(ScreenNews.class, "onStart");
		
	}

	@Override
	protected void onStop() {
		super.onStop();
		RssReaderApplication.LogD(ScreenNews.class, "onStop");
	}
}
